﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreBDS;
using System.IO;

namespace BatDongSan.Manager
{
    public partial class ManBrokerUpdate : System.Web.UI.Page
    {
        public static int _id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _id = Convert.ToInt32(Request.QueryString["id"]);
                if (_id > 0)
                    LoadInfo(_id);
                else btnUpdate.Text = "Thêm mới";
            }
        }
        public void LoadInfo(int id)
        {
            try
            {
                //Load Vietnammes
                Cvk_Broker info = new Cvk_Broker();
                info = BrokerBll.GetInfo(id, Utility.strConnect);
                if (info != null)
                {
                    HiddenField1.Value = info.Img;
                    txtName.Text = info.Name;
                    fckContent.Value = info.Description;
                    ddlVIP.SelectedValue = Convert.ToString(info.VipId);
                    txtAddress.Text = info.Address;
                    txtPhone.Text = info.Phone;
                    txtFax.Text = info.Fax;
                    txtTaxCode.Text = info.TaxCode;
                    txtEmail.Text = info.Email;
                    txtWebsite.Text = info.Website;
                    ckbActive.Checked = Convert.ToBoolean(info.IsActive);
                }
            }
            catch { }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtName.Text))
                {
                    lbStatus.Text = "Vui lòng nhập đầy đủ thông tin cần thiết.";
                    txtName.Focus();
                    return;
                }
                //Save Info Vietnammes
                Cvk_Broker info = new Cvk_Broker();
                info.BrokerId = _id;
                info.Name = txtName.Text;
                info.Description = fckContent.Value;
                info.VipId = Convert.ToInt32(ddlVIP.SelectedValue);
                info.Address = txtAddress.Text;
                info.Phone = txtPhone.Text;
                info.Fax = txtFax.Text;
                info.TaxCode = txtTaxCode.Text;
                info.Email = txtEmail.Text;
                info.Website = txtWebsite.Text;
                info.IsActive = ckbActive.Checked;

                #region Upload Image Thumb
                string fileName1 = string.Empty;
                string filePath1 = Server.MapPath(string.Format("~/Images/Moigioi/"));
                try
                {
                    if (!Directory.Exists(filePath1))
                    {
                        Directory.CreateDirectory(filePath1);
                    }
                    if (FileUpload1.HasFile)
                    {
                        if (cvk.AppCode.WebUtility.IsValidFile(FileUpload1.FileName, ".jpg", ".jpeg", ".gif", ".bmp") && FileUpload1.FileContent.Length <= 3145728) //3M
                        {
                            fileName1 = cvk.AppCode.WebUtility.GetDateTimeString(FileUpload1.FileName);
                            cvk.AppCode.WebUtility.UploadImage(FileUpload1, filePath1, fileName1, 150, 150);
                            //FileUpload1.SaveAs(filePath1 + fileName1);
                        }
                        else
                        {
                            lbStatus.Text = "Hình Upload không hợp lệ, vui lòng chọn hình ảnh khác.";
                            return;
                        }
                    }
                    else if (_id == 0)
                    {
                        lbStatus.Text = "Vui lòng chọn hình ảnh đại diện.";
                        return;
                    }
                    else fileName1 = HiddenField1.Value;
                }
                catch
                { }
                info.Img = fileName1;
                #endregion

                if (_id > 0)
                {
                    BrokerBll.Update(info, Utility.strConnect);
                    lbStatus.Text = "Quá trình cập nhật thành công.";
                }
                else
                {

                    info.IsDeleted = false;
                    info.CreateDate = DateTime.Now;
                    int id = BrokerBll.Insert(info, Utility.strConnect);
                    lbStatus.Text = "Quá trình thêm mới thành công.";
                }
                //Reset();
            }
            catch (Exception ex)
            {
                lbStatus.Text = ex.Message;
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        public void Reset()
        {
            txtName.Text = fckContent.Value = txtAddress.Text = txtEmail.Text = txtFax.Text = txtName.Text = txtPhone.Text = txtTaxCode.Text = txtWebsite.Text = string.Empty;
            ckbActive.Checked = false;
            HiddenField1.Value = string.Empty;
            _id = 0;
            btnUpdate.Text = "Thêm mới";
        }
    }
}